﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Autofac;
using Autofac.Core.Lifetime;
using Microsoft.Extensions.DependencyInjection;

namespace Util
{
    /// <summary>
    /// 一个负责创建对象的引擎
    /// </summary>
    public interface IEngine
    {
        T Resolve<T>();
    }

    /// <summary>
    /// 引擎实现类
    /// </summary>
    public class GeneralEngine : IEngine
    {
        private IServiceProvider _provider;
        public GeneralEngine(IServiceProvider provider)
        {
            this._provider = provider;
        }
        /// <summary>
        /// 构建实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Resolve<T>()
        {
            return _provider.GetService<T>();
        }
    }

    /// <summary>
    /// 引擎上下文,在项目初始化过程中实例化一个引擎实例
    /// </summary>
    public class EngineContext
    {
        private static ILifetimeScope _engine;
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static ILifetimeScope initialize(ILifetimeScope engine)
        {
            if (_engine == null)
            {
                _engine = engine;
            }
            return _engine;
        }
        /// <summary>
        /// 当前引擎
        /// </summary>
        public static ILifetimeScope Current
        {
            get
            {
                return _engine;
            }
        }

        /// <summary>
        /// Resolve
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="scope">Scope; pass null to automatically resolve the current scope</param>
        /// <returns>Resolved service</returns>
        public static T Resolve<T>(ILifetimeScope scope = null)
        {
            Type type = typeof(T);
            if (scope == null)
            {
                //no scope specified
                scope = Scope();
            }
            var obj = scope.Resolve(type);
            if (obj != null && obj is T)
                return (T)obj;
            else
                return default(T);
        }

        /// <summary>
        /// Get current scope
        /// </summary>
        /// <returns>Scope</returns>
        public static ILifetimeScope Scope()
        {
            try
            {
                //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
                return _engine.BeginLifetimeScope();
            }
            catch (Exception)
            {
                //we can get an exception here if RequestLifetimeScope is already disposed
                //for example, requested in or after "Application_EndRequest" handler
                //but note that usually it should never happen

                //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
                return _engine.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
            }
        }
    }
}
